Spatial Data and Mapping in R

Jon Minton

2022-05-23

Aim

Theory - geometries

theory - data structures

The sf package

Example with sf

Let’s try the example code in the above intro

pacman::p_load(tidyverse, sf)

nc <- st_read(system.file("shape/nc.shp", package="sf"))
## Reading layer `nc' from data source 
##   `C:\Users\Jon Minton\AppData\Local\R\win-library\4.2\sf\shape\nc.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 100 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
## Geodetic CRS:  NAD27
class(nc)
## [1] "sf"         "data.frame"
nc
## Simple feature collection with 100 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
## Geodetic CRS:  NAD27
## First 10 features:
##     AREA PERIMETER CNTY_ CNTY_ID        NAME  FIPS FIPSNO CRESS_ID BIR74 SID74
## 1  0.114     1.442  1825    1825        Ashe 37009  37009        5  1091     1
## 2  0.061     1.231  1827    1827   Alleghany 37005  37005        3   487     0
## 3  0.143     1.630  1828    1828       Surry 37171  37171       86  3188     5
## 4  0.070     2.968  1831    1831   Currituck 37053  37053       27   508     1
## 5  0.153     2.206  1832    1832 Northampton 37131  37131       66  1421     9
## 6  0.097     1.670  1833    1833    Hertford 37091  37091       46  1452     7
## 7  0.062     1.547  1834    1834      Camden 37029  37029       15   286     0
## 8  0.091     1.284  1835    1835       Gates 37073  37073       37   420     0
## 9  0.118     1.421  1836    1836      Warren 37185  37185       93   968     4
## 10 0.124     1.428  1837    1837      Stokes 37169  37169       85  1612     1
##    NWBIR74 BIR79 SID79 NWBIR79                       geometry
## 1       10  1364     0      19 MULTIPOLYGON (((-81.47276 3...
## 2       10   542     3      12 MULTIPOLYGON (((-81.23989 3...
## 3      208  3616     6     260 MULTIPOLYGON (((-80.45634 3...
## 4      123   830     2     145 MULTIPOLYGON (((-76.00897 3...
## 5     1066  1606     3    1197 MULTIPOLYGON (((-77.21767 3...
## 6      954  1838     5    1237 MULTIPOLYGON (((-76.74506 3...
## 7      115   350     2     139 MULTIPOLYGON (((-76.00897 3...
## 8      254   594     2     371 MULTIPOLYGON (((-76.56251 3...
## 9      748  1190     2     844 MULTIPOLYGON (((-78.30876 3...
## 10     160  2038     5     176 MULTIPOLYGON (((-80.02567 3...

Note the geometry column

example with sf - continued

print(nc[9:15], n = 3)
## Simple feature collection with 100 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
## Geodetic CRS:  NAD27
## First 3 features:
##   BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79                       geometry
## 1  1091     1      10  1364     0      19 MULTIPOLYGON (((-81.47276 3...
## 2   487     0      10   542     3      12 MULTIPOLYGON (((-81.23989 3...
## 3  3188     5     208  3616     6     260 MULTIPOLYGON (((-80.45634 3...

example with sf - continued

Pull out the geometry column

(nc_geom <- st_geometry(nc))
## Geometry set for 100 features 
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
## Geodetic CRS:  NAD27
## First 5 geometries:
## MULTIPOLYGON (((-81.47276 36.23436, -81.54084 3...
## MULTIPOLYGON (((-81.23989 36.36536, -81.24069 3...
## MULTIPOLYGON (((-80.45634 36.24256, -80.47639 3...
## MULTIPOLYGON (((-76.00897 36.3196, -76.01735 36...
## MULTIPOLYGON (((-77.21767 36.24098, -77.23461 3...
nc_geom[[1]]
## MULTIPOLYGON (((-81.47276 36.23436, -81.54084 36.27251, -81.56198 36.27359, -81.63306 36.34069, -81.74107 36.39178, -81.69828 36.47178, -81.7028 36.51934, -81.67 36.58965, -81.3453 36.57286, -81.34754 36.53791, -81.32478 36.51368, -81.31332 36.4807, -81.26624 36.43721, -81.26284 36.40504, -81.24069 36.37942, -81.23989 36.36536, -81.26424 36.35241, -81.32899 36.3635, -81.36137 36.35316, -81.36569 36.33905, -81.35413 36.29972, -81.36745 36.2787, -81.40639 36.28505, -81.41233 36.26729, -81.43104 36.26072, -81.45289 36.23959, -81.47276 36.23436)))

plotting

plot(nc_geom[[1]])

plot(nc_geom[[2]])

plot(nc_geom)

example with sf - continued

plot(nc)
## Warning: plotting the first 10 out of 14 attributes; use max.plot = 14 to plot
## all

# We can use tidyverse functions with the nc object (as it's a data.frame)
nc %>% 
  arrange(desc(AREA))
## Simple feature collection with 100 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
## Geodetic CRS:  NAD27
## First 10 features:
##     AREA PERIMETER CNTY_ CNTY_ID      NAME  FIPS FIPSNO CRESS_ID BIR74 SID74
## 1  0.241     2.214  2083    2083   Sampson 37163  37163       82  3025     4
## 2  0.240     2.004  2150    2150   Robeson 37155  37155       78  7889    31
## 3  0.240     2.365  2232    2232  Columbus 37047  37047       24  3350    15
## 4  0.225     2.107  2162    2162    Bladen 37017  37017        9  1782     8
## 5  0.219     2.130  1938    1938      Wake 37183  37183       92 14484    16
## 6  0.214     2.152  2185    2185    Pender 37141  37141       71  1228     4
## 7  0.212     2.024  2241    2241 Brunswick 37019  37019       10  2181     5
## 8  0.207     1.851  1989    1989  Johnston 37101  37101       51  3999     6
## 9  0.204     1.871  2100    2100    Duplin 37061  37061       31  2483     4
## 10 0.203     3.197  2004    2004  Beaufort 37013  37013        7  2692     7
##    NWBIR74 BIR79 SID79 NWBIR79                       geometry
## 1     1396  3447     4    1524 MULTIPOLYGON (((-78.11377 3...
## 2     5904  9087    26    6899 MULTIPOLYGON (((-78.86451 3...
## 3     1431  4144    17    1832 MULTIPOLYGON (((-78.65572 3...
## 4      818  2052     5    1023 MULTIPOLYGON (((-78.2615 34...
## 5     4397 20857    31    6221 MULTIPOLYGON (((-78.92107 3...
## 6      580  1602     3     763 MULTIPOLYGON (((-78.02592 3...
## 7      659  2655     6     841 MULTIPOLYGON (((-78.65572 3...
## 8     1165  4780    13    1349 MULTIPOLYGON (((-78.53874 3...
## 9     1061  2777     7    1227 MULTIPOLYGON (((-77.68983 3...
## 10    1131  2909     4    1163 MULTIPOLYGON (((-77.10377 3...

Plotting with ggplot

ggplot(data = nc) +
  geom_sf()

We can use the fill attribute. For example, let’s make the fill dependent on the ratio of perimeter to area, so wigglier/more connected places are hotter colours r

nc %>% 
  mutate(
    wiggliness = PERIMETER / AREA
  ) %>% 
  ggplot() +
  geom_sf(aes(fill = wiggliness), color = NA)

Plotting with tmap

tmap is quite similar to ggplot2 in operation, but just focused on maps, and having defaults that work well with spatial data

pacman::p_load(tmap)

qtm(nc) # qtm : 'quick tmap', analogous to ggplot2::qplot() 

And to fill polygons based on a value

tm_shape(nc) + tm_fill("AREA") # note area in quotes, also use of tm_shape first

nc %>% 
  mutate(
    wiggliness = PERIMETER / AREA
  ) %>% 
  tm_shape() + 
  tm_fill("wiggliness")

Plotting with interactive tmap

tmap_mode("view")
## tmap mode set to interactive viewing
nc %>% 
  mutate(
    wiggliness = PERIMETER / AREA
  ) %>% 
  tm_shape() + 
  tm_fill("wiggliness")

Note the addition of a standard base map layer.

to switch back between modes

ttm() # toggles between interactive and static
## tmap mode set to plotting
tmap_mode("plot") # switches to static (even if already on static mode)
## tmap mode set to plotting

tmap basemaps

When in view mode (using leaflet), different basemaps can be selected by specifying a valid selection from leaflet::providers). Here’s the default example:

current.mode <- tmap_mode("view")
## tmap mode set to interactive viewing
data(World, metro)

tm_basemap(leaflet::providers$Stamen.Watercolor) +
# tm_basemap(leaflet::providers$CartoDB) +
  tm_shape(metro, bbox = "India") + tm_dots(col = "red", group = "Metropolitan areas") +
  tm_tiles(paste0("http://services.arcgisonline.com/arcgis/rest/services/Canvas/",
      "World_Light_Gray_Reference/MapServer/tile/{z}/{y}/{x}"), group = "Labels")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()

Both the basemap and tiles are rasters (bitmap image files). The difference is that tiles go on top and the basemap is at the bottom. (maps being composed of successive layers, much like ggplots)

Application to Scottish data

defib_points <- st_read("Defibrillators/Defibrillators.shp")
## Reading layer `Defibrillators' from data source 
##   `C:\Users\Jon Minton\repos\tardy_tuesday_spatial\Defibrillators\Defibrillators.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 124 features and 5 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 266833 ymin: 696948 xmax: 326107 ymax: 766039
## Projected CRS: OSGB 1936 / British National Grid

Scottish example continued

Maybe we can use leaflet with the view mode to see where these are

current.mode <- tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap(leaflet::providers$Stamen.Watercolor) +
# tm_basemap(leaflet::providers$CartoDB) +
  tm_shape(defib_points) + tm_dots()

Scottish example continued

current.mode <- tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap(leaflet::providers$OpenStreetMap.Mapnik) +
# tm_basemap(leaflet::providers$CartoDB) +
  tm_shape(defib_points) + tm_dots()

With SIMD

We can also attempt to combine layers of different types

There are neater ways of doing this, but I’ve downloaded the SIMD 2020 shapefiles here

simd_shapes <- st_read("big_data/SG_SIMD_2020/SG_SIMD_2020.shp")
## Reading layer `SG_SIMD_2020' from data source 
##   `C:\Users\Jon Minton\repos\tardy_tuesday_spatial\big_data\SG_SIMD_2020\SG_SIMD_2020.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 6976 features and 51 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 5513 ymin: 530252.8 xmax: 470323 ymax: 1220302
## Projected CRS: OSGB 1936 / British National Grid
simd_shapes
## Simple feature collection with 6976 features and 51 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 5513 ymin: 530252.8 xmax: 470323 ymax: 1220302
## Projected CRS: OSGB 1936 / British National Grid
## First 10 features:
##     DataZone                                    DZName        LAName SAPE2017
## 1  S01006506                               Culter - 01 Aberdeen City      894
## 2  S01006507                               Culter - 02 Aberdeen City      793
## 3  S01006508                               Culter - 03 Aberdeen City      624
## 4  S01006509                               Culter - 04 Aberdeen City      537
## 5  S01006510                               Culter - 05 Aberdeen City      663
## 6  S01006511                               Culter - 06 Aberdeen City      759
## 7  S01006512                               Culter - 07 Aberdeen City      539
## 8  S01006513 Cults, Bieldside and Milltimber West - 01 Aberdeen City      788
## 9  S01006514 Cults, Bieldside and Milltimber West - 02 Aberdeen City     1123
## 10 S01006515 Cults, Bieldside and Milltimber West - 03 Aberdeen City      816
##    WAPE2017 Rankv2 Quintilev2 Decilev2 Vigintilv2 Percentv2 IncRate IncNumDep
## 1       580   4691          4        7         14        68      8%        71
## 2       470   4862          4        7         14        70      5%        43
## 3       461   5686          5        9         17        82      6%        40
## 4       307   4332          4        7         13        63     10%        52
## 5       415   3913          3        6         12        57     10%        68
## 6       453   6253          5        9         18        90      4%        30
## 7       345   5692          5        9         17        82      2%        13
## 8       406   6177          5        9         18        89      2%        14
## 9       709   6715          5       10         20        97      2%        17
## 10      529   6363          5       10         19        92      1%         5
##    IncRankv2 EmpRate EmpNumDep EmpRank HlthCIF HlthAlcSR HlthDrugSR HlthSMR
## 1       3936      8%        49    3220      65        29         30      70
## 2       4829      5%        25    4481      45       130        126      81
## 3       4460      4%        19    5110      45        71         18      41
## 4       3481      8%        26    3229      80        80         28     103
## 5       3344      8%        32    3448      95        89         44     139
## 6       5469      4%        17    5346      50        55          0      54
## 7       6264      2%         8    6206      40        29          0      41
## 8       6572      3%        13    5695      40        28          0     138
## 9       6704      2%        12    6658      25        18          0     107
## 10      6955      1%         7    6803      25        23          0      34
##    HlthDprsPc HlthLBWTPc HlthEmergS HlthRank EduAttend EduAttain EduNoQuals
## 1         13%         0%         74     5174       85%      5.88         53
## 2         14%         0%         86     5051       85%      5.96         96
## 3         13%         4%         69     5942       90%      5.75         39
## 4         16%         5%         88     3871       94%      6.20         80
## 5         22%         5%         89     3049       80%      5.87         77
## 6         12%        13%         73     5783       90%      5.79         54
## 7         11%         6%         55     6586       93%      5.86         27
## 8         21%         0%         72     5516       94%      6.10         40
## 9         12%         8%         59     6733       94%      6.50         19
## 10        10%         0%         58     6846       96%      6.00         15
##    EduPartici EduUniver EduRank GAccPetrol GAccDTGP GAccDTPost GAccDTPsch
## 1          0%       30%    5887       2.54     3.07       1.62       2.62
## 2          2%       12%    4384       3.92     4.31       2.56       3.65
## 3          1%       19%    5915       3.32     3.78       1.44       3.25
## 4          0%       25%    6401       2.62     2.78       2.62       1.94
## 5          6%       16%    4092       2.12     2.36       2.41       1.85
## 6          2%       13%    5410       1.52     1.77       3.23       2.52
## 7          0%       15%    6506       5.37     5.78       3.91       5.37
## 8          3%       28%    6531       3.57     4.32       5.52       1.95
## 9          1%       22%    6847       2.72     3.52       4.69       2.89
## 10         0%       20%    6838       4.44     5.18       6.34       3.06
##    GAccDTSsch GAccDTRet GAccPTGP GAccPTPost GAccPTRet GAccBrdbnd GAccRank
## 1        9.93      1.54     8.86       5.86      6.02        11%     4724
## 2       11.04      2.85     9.98       7.52      7.93         1%     2148
## 3       10.62      2.06     8.62       4.32      5.77         1%     4200
## 4       10.04      2.16     7.94       8.43      8.33        11%     3982
## 5        9.65      1.78     5.57       6.97      6.63         0%     5588
## 6        8.61      2.59     4.93       7.67      7.34         5%     4974
## 7       12.22      4.28    19.01      16.26     16.96        54%      547
## 8        6.26      4.91    11.27      10.98     11.10         3%     1490
## 9        7.04      4.06     9.74      10.00      9.73        14%     1858
## 10       6.88      5.78    17.18      17.09     17.03        22%      659
##    CrimeCount CrimeRate CrimeRank HouseNumOC HouseNumNC HouseOCrat HouseNCrat
## 1          11       125    4664.0         87         10        10%         1%
## 2          10       128    4602.0         85          4        10%         0%
## 3           8       130    4563.5         31          8         5%         1%
## 4           4        75    5626.0         42          6         7%         1%
## 5          11       168    3885.0         50          7         9%         1%
## 6           0         0    6928.0         27          8         4%         1%
## 7           7       132    4528.0         27          9         5%         2%
## 8           0         0    6928.0         15          4         3%         1%
## 9           9        81    5507.0         10          3         1%         0%
## 10          0         0    6844.0         29          1         4%         0%
##    HouseRank Shape_Leng  Shape_Area                       geometry
## 1     3248.0  11801.872  4388802.12 MULTIPOLYGON (((383285.3 80...
## 2     3486.0   2900.406   221746.84 MULTIPOLYGON (((383527.9 80...
## 3     5342.0   3468.762   270194.75 MULTIPOLYGON (((383473 8012...
## 4     4394.5   1647.461    96254.26 MULTIPOLYGON (((383976.7 80...
## 5     3736.0   3026.111   180076.58 MULTIPOLYGON (((384339 8012...
## 6     5924.0   4300.089   400488.04 MULTIPOLYGON (((384737 8013...
## 7     4815.0  22464.858 13996149.90 MULTIPOLYGON (((379360.2 80...
## 8     6394.0   4077.120   300877.30 MULTIPOLYGON (((386702 8018...
## 9     6868.0   8064.497  1877298.26 MULTIPOLYGON (((385842.5 80...
## 10    6174.5  15374.998  5542698.36 MULTIPOLYGON (((386014 8033...

Maybe we just want to find the right LA

unique(simd_shapes$LAName)
##  [1] "Aberdeen City"         "Aberdeenshire"         "Angus"                
##  [4] "Argyll and Bute"       "Clackmannanshire"      "Dumfries and Galloway"
##  [7] "Dundee City"           "East Ayrshire"         "East Dunbartonshire"  
## [10] "East Lothian"          "East Renfrewshire"     "City of Edinburgh"    
## [13] "Na h-Eileanan an Iar"  "Falkirk"               "Fife"                 
## [16] "Glasgow City"          "Highland"              "Inverclyde"           
## [19] "Midlothian"            "Moray"                 "North Ayrshire"       
## [22] "North Lanarkshire"     "Orkney Islands"        "Perth and Kinross"    
## [25] "Renfrewshire"          "Scottish Borders"      "Shetland Islands"     
## [28] "South Ayrshire"        "South Lanarkshire"     "Stirling"             
## [31] "West Dunbartonshire"   "West Lothian"
simd_pc <- simd_shapes %>% 
  filter(LAName == "Perth and Kinross")

simd_pc
## Simple feature collection with 186 features and 51 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 233337.5 ymin: 694491.9 xmax: 335270.4 ymax: 785825.5
## Projected CRS: OSGB 1936 / British National Grid
## First 10 features:
##     DataZone                                DZName            LAName SAPE2017
## 1  S01011833 Powmill, Cleish and Scotlandwell - 01 Perth and Kinross     1014
## 2  S01011834 Powmill, Cleish and Scotlandwell - 02 Perth and Kinross      577
## 3  S01011835 Powmill, Cleish and Scotlandwell - 03 Perth and Kinross      926
## 4  S01011836 Powmill, Cleish and Scotlandwell - 04 Perth and Kinross      851
## 5  S01011837 Powmill, Cleish and Scotlandwell - 05 Perth and Kinross      712
## 6  S01011838                          Kinross - 01 Perth and Kinross      893
## 7  S01011839                          Kinross - 02 Perth and Kinross      974
## 8  S01011840                          Kinross - 03 Perth and Kinross     1114
## 9  S01011841                          Kinross - 04 Perth and Kinross      990
## 10 S01011842                          Kinross - 05 Perth and Kinross      935
##    WAPE2017 Rankv2 Quintilev2 Decilev2 Vigintilv2 Percentv2 IncRate IncNumDep
## 1       622   4853          4        7         14        70      5%        49
## 2       345   5883          5        9         17        85      4%        21
## 3       604   5011          4        8         15        72      5%        49
## 4       494   5232          4        8         15        75      4%        32
## 5       462   5627          5        9         17        81      2%        15
## 6       504   6940          5       10         20       100      2%        16
## 7       540   6790          5       10         20        98      2%        18
## 8       666   5800          5        9         17        84      6%        63
## 9       578   4413          4        7         13        64      8%        82
## 10      555   3512          3        6         11        51     11%       104
##    IncRankv2 EmpRate EmpNumDep EmpRank HlthCIF HlthAlcSR HlthDrugSR HlthSMR
## 1     5074.0      4%        26  5032.0      55        24         17     102
## 2     5642.0      3%        10  5862.0      50         7         60      51
## 3     4891.0      3%        19  5710.0      50        20          0      53
## 4     5574.0      4%        18  5370.0      40         4         23      66
## 5     6434.0      3%        12  6105.0      30        39         24      72
## 6     6566.0      2%        11  6390.0      30        20         21      56
## 7     6546.0      3%        14  6082.5      40         7         35      61
## 8     4741.0      4%        24  5411.0      60        14         61      51
## 9     3840.5      6%        37  3935.0      95        58        117      71
## 10    3133.0     10%        53  2826.0      95        84        287      84
##    HlthDprsPc HlthLBWTPc HlthEmergS HlthRank EduAttend EduAttain EduNoQuals
## 1         15%         0%         73     5300       88%      5.82         34
## 2          9%         5%         63     6336       92%      5.86         42
## 3         16%         6%         65     5863       81%      6.08         30
## 4         14%         0%         71     6252       95%      5.96         44
## 5         10%         0%         53     6697       90%      6.00         53
## 6         14%         0%         63     6557       94%      6.27         31
## 7         14%         9%         64     6224       89%      5.95         42
## 8         16%         7%         81     5083       86%      5.72         70
## 9         17%        11%        100     3392       82%      5.79         70
## 10        20%         6%        117     2729       89%      5.76        129
##    EduPartici EduUniver EduRank GAccPetrol GAccDTGP GAccDTPost GAccDTPsch
## 1          0%       17%    6013       5.24     9.48       4.19       3.49
## 2          1%       17%    6050       4.25     4.17       3.99       4.23
## 3          3%       14%    5532       4.03     5.62       5.46       5.04
## 4          1%       22%    6490       2.63     7.68       6.79       2.84
## 5          1%       17%    5796       3.92     6.19       5.91       5.21
## 6          2%       20%    6615       2.92     1.19       2.34       3.14
## 7          3%       11%    5472       2.93     2.09       3.05       3.29
## 8          2%       11%    4552       2.11     2.03       2.27       2.35
## 9          2%       12%    4369       3.80     3.72       1.65       2.95
## 10         1%       16%    4436       2.57     2.71       1.26       1.84
##    GAccDTSsch GAccDTRet GAccPTGP GAccPTPost GAccPTRet GAccBrdbnd GAccRank
## 1       11.26      8.58    20.51      12.41     18.51        26%      485
## 2        5.36      4.07    10.62       9.60      9.87        56%     1221
## 3        9.89      7.70    14.80      14.88     19.12        61%      512
## 4        9.95     10.01    19.80      16.50     26.19        23%      502
## 5        8.77      8.82    23.34      19.67     32.71        33%      389
## 6        2.67      2.51     4.57       6.10      6.67         8%     5858
## 7        3.52      3.07     5.35       9.57     10.53         0%     4884
## 8        3.68      2.29     7.78       9.16      9.24         0%     5537
## 9        5.16      1.68    10.71       5.78      5.86         1%     4607
## 10       4.09      1.28     8.20       4.58      4.64         1%     6255
##    CrimeCount CrimeRate CrimeRank HouseNumOC HouseNumNC HouseOCrat HouseNCrat
## 1           5        51      6150         18         28         2%         3%
## 2           0         0      6453          3          3         1%         1%
## 3           5        56      6058         31         10         5%         1%
## 4           9       109      4955         22          8         3%         1%
## 5           0         0      6808          7          9         1%         1%
## 6           9       104      5039         20          8         2%         1%
## 7           3        32      6531         40          1         4%         0%
## 8           4        37      6432         50          8         5%         1%
## 9          13       135      4458         65          6         7%         1%
## 10         20       209      3267        131          7        14%         1%
##    HouseRank Shape_Leng Shape_Area                       geometry
## 1       5868  33413.287 24102701.3 MULTIPOLYGON (((302460.5 70...
## 2       6890  34190.960 16499300.0 MULTIPOLYGON (((311407.8 70...
## 3       5379  42221.932 28189611.8 MULTIPOLYGON (((314131.4 69...
## 4       6163  45152.024 17637636.0 MULTIPOLYGON (((316108.8 70...
## 5       6626  47123.950 28699147.7 MULTIPOLYGON (((313323.5 70...
## 6       6339   6301.371   379230.7 MULTIPOLYGON (((311472.4 70...
## 7       6065   3779.878   229797.3 MULTIPOLYGON (((311321 7034...
## 8       5468   7100.581   596025.5 MULTIPOLYGON (((311166 7031...
## 9       4726   5175.039   445845.7 MULTIPOLYGON (((311459.2 70...
## 10      2353   4032.762   263301.4 MULTIPOLYGON (((311398 7026...
# The bounding box of the filtered and unfiltered selection 
st_bbox(simd_pc)
##     xmin     ymin     xmax     ymax 
## 233337.5 694491.9 335270.4 785825.5
st_bbox(simd_shapes)
##      xmin      ymin      xmax      ymax 
##    5513.0  530252.8  470323.0 1220301.5
# So, the bounding box is narrowed when the selection is narrowed
tmap_mode("plot")
## tmap mode set to plotting
qtm(simd_pc)

tm_shape(simd_pc) + 
  tm_polygons(col = "HlthSMR")

Combining

Let’s now try to combine both datasets

tmap_mode("plot")
## tmap mode set to plotting
tm_shape(simd_pc) + 
  tm_polygons(col = "HlthSMR") + 
  tm_shape(defib_points) + 
  tm_dots(col = "red")

Or interactive…

tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap(leaflet::providers$OpenStreetMap.Mapnik) + 
  tm_shape(simd_pc) + 
    tm_polygons(col = "HlthSMR", alpha = 0.3) + # set transparency 
    tm_shape(defib_points) + 
    tm_dots(col = "red")

Conclusion/Discussion

Not covered